package com.lhh.userservice.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lhh.springcommon.annotation.WebLog;
import com.lhh.springcommon.entity.Like;
import com.lhh.springcommon.service.ILikeService;
import com.lhh.springcommon.interceptor.JwtInterceptor;
import com.lhh.userservice.service.UserLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 李厚航
 * @date 2025-03-01 22:02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserLikeServiceImpl implements UserLikeService {

    private final ILikeService likeService;

    @Autowired
    public UserLikeServiceImpl(ILikeService likeService) {
        this.likeService = likeService;
    }

    @Override
    @WebLog
    @CacheEvict(value = "countLike",allEntries = true)
    public Boolean addOrDeleteLike(Like like) {
        LambdaQueryWrapper<Like> eq = new LambdaQueryWrapper<Like>()
                .eq(Like::getUid, like.getUid())
                .eq(Like::getVid, like.getVid());
        if(likeService.getOne(eq) == null){
            return likeService.save(like);
        }
        return likeService.remove(eq);
    }

    @Override
    @WebLog
    public Boolean isLike(Integer uid,Integer vid) {
        return likeService.getOne(new LambdaQueryWrapper<Like>()
                .eq(Like::getUid, uid)
                .eq(Like::getVid, vid)) != null;
    }

    @Override
    @WebLog
    public Long countLike(Long vid) {
        return likeService.count(new LambdaQueryWrapper<Like>()
                .eq(Like::getVid, vid));
    }
}
